/*
 * Project: Set Sample
 * Copyright (C) 2013 alf.labs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alflabs.setsample.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.TextView;
import com.alflabs.annotations.Null;
import com.alflabs.setsample.BuildConfig;
import com.alflabs.setsample.R;
import com.alflabs.setsample.core.SetPrefsValues;
import com.alflabs.setsample.stats.GAHelper;

public class SetActivity extends ActionBarActivity implements SetCompletedDialog.ICompletedDialogCallback {

    private static final boolean DEBUG = BuildConfig.DEBUG;
    private static final String TAG = SetActivity.class.getSimpleName();
    private @Null TextView mTimeCounter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_activity);

        // Show the Up button in the action bar.
        ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setDisplayHomeAsUpEnabled(true);

            SetPrefsValues pv = new SetPrefsValues(this);
            if (pv.isShowTimeCounter()) {
                mTimeCounter = (TextView) getLayoutInflater().inflate(R.layout.set_board_counter, null);
                if (mTimeCounter != null) {
                    ab.setDisplayShowCustomEnabled(true);

                    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
                    lp.gravity = Gravity.CENTER;

                    ab.setCustomView(mTimeCounter, lp);
                }
            }
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (mTimeCounter != null && fragment instanceof SetBoardFragment) {
            ((SetBoardFragment) fragment).setTimeCounterView(mTimeCounter);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mTimeCounter != null) {
            Fragment f = getSupportFragmentManager().findFragmentById(R.id.set_board_fragment);
            if (f instanceof SetBoardFragment) {
                ((SetBoardFragment) f).setTimeCounterView(mTimeCounter);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        GAHelper.activityStart(this);  // GA
    }

    @Override
    protected void onStop() {
        super.onStop();
        GAHelper.activityStop(this);   // GA
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Fragment f = getSupportFragmentManager().findFragmentById(R.id.set_board_fragment);
        if (f instanceof SetBoardFragment) {
            ((SetBoardFragment) f).onWindowFocusChanged(hasFocus);
        }
    }

    @Override
    public void onCompletedDialogCancel() {
        Fragment f = getSupportFragmentManager().findFragmentById(R.id.set_board_fragment);
        if (f instanceof SetBoardFragment) {
            ((SetBoardFragment) f).onCompletedDialogCancel();
        }
    }

    @Override
    public void onCompletedDialogNewBoard() {
        Fragment f = getSupportFragmentManager().findFragmentById(R.id.set_board_fragment);
        if (f instanceof SetBoardFragment) {
            ((SetBoardFragment) f).onCompletedDialogNewBoard();
        }
    }
}
